#!/usr/bin/env python3
"""
数据源基础类
定义数据源的基础接口和状态
"""

import abc
from typing import Dict, List, Any, Optional
import pandas as pd
from datetime import datetime
from enum import Enum


class DataSourceStatus(Enum):
    """数据源状态"""
    AVAILABLE = "available"      # 可用
    LIMITED = "limited"          # 受限（如频率限制）
    UNAVAILABLE = "unavailable"  # 不可用
    ERROR = "error"              # 错误状态


class BaseDataSource(abc.ABC):
    """数据源基础类"""

    def __init__(self, name: str, config: Dict[str, Any] = None):
        self.name = name
        self.config = config or {}
        self.status = DataSourceStatus.UNAVAILABLE
        self.last_error = None
        self.last_check_time = None

    def update_status(self, status: DataSourceStatus, error_message: str = None):
        """更新数据源状态"""
        self.status = status
        self.last_error = error_message
        self.last_check_time = datetime.now()

    @abc.abstractmethod
    def check_availability(self) -> bool:
        """检查数据源是否可用"""
        pass

    @abc.abstractmethod
    def get_stock_list(self) -> pd.DataFrame:
        """获取股票列表"""
        pass

    @abc.abstractmethod
    def fetch_history_data(self, symbol: str, interval: str,
                          start_date: str, end_date: str) -> pd.DataFrame:
        """获取历史数据"""
        pass

    @abc.abstractmethod
    def get_stock_info(self, symbol: str) -> Dict[str, Any]:
        """获取股票信息"""
        pass

    def get_status_info(self) -> Dict[str, Any]:
        """获取状态信息"""
        return {
            'name': self.name,
            'status': self.status.value,
            'last_error': self.last_error,
            'last_check_time': self.last_check_time.isoformat() if self.last_check_time else None
        }
